/* eslint-disable */
/*
 * @Descripttion: 常用工具类
 * @version: 1.0.0
 * @Author: shuaishuai.han
 * @Date: 2022-01-07 17:04:30
 */

/**
 * @name: 钱保留两位小数 不足补0
 * @param {String | Number} value 要处理的数据
 * @param {Number} tofixed 保留几位小数
 * @return {*}
 */
export const toDecimal = (value) => {
  const num = parseFloat(value);
  if (isNaN(num)) return;
  let rn = (parseInt(num * 100) / 100).toString();
  let rs = rn.indexOf('.');
  if (rs < 0) {
    rs = rn.length;
    rn += '.';
  }
  while (rn.length <= rs + 2) {
    rn += '0';
  }
  return rn;
};

/**
 * @name: 获取地址栏参数
 * @param {string} name 需要获取的参数
 * @return {*}
 */
export const GetQueryString = (name) => {
  let reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`, 'i');
  let r = location.search.substr(1).match(reg);
  if (r != null) return unescape(decodeURI(r[2]));
  return null;
};

/**
 * @name: 根据身份证号提取年龄
 * @param {String} iden身份证号
 * @return {*}
 */
export const getAge = (iden) => {
  let val = iden.length;
  let myDate = new Date();
  let month = myDate.getMonth() + 1;
  let day = myDate.getDate();
  let age = 0;
  if (val === 18) {
    age = myDate.getFullYear() - iden.substring(6, 10) - 1;
    if (
      iden.substring(10, 12) < month ||
      (iden.substring(10, 12) == month && iden.substring(12, 14) <= day)
    ) {
      age++;
    }
  }
  return age;
};

/**
 * @name: 日期格式化
 * @param {Date} objDate 'new Date()'
 * @param {string} fmt 'yyyy-MM-dd HH:mm:ss'(传入的格式)
 * @return {*}
 */
export const formatDate = (objDate, fmt) => {
  const o = {
    'M+': objDate.getMonth() + 1, // 月份
    'd+': objDate.getDate(), // 日
    'h+': objDate.getHours() % 12 === 0 ? 12 : objDate.getHours() % 12, // 小时
    'H+': objDate.getHours(), // 小时
    'm+': objDate.getMinutes(), // 分
    's+': objDate.getSeconds(), // 秒
    'q+': Math.floor((objDate.getMonth() + 3) / 3), // 季度
    S: objDate.getMilliseconds() // 毫秒
  };
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(
      RegExp.$1,
      `${objDate.getFullYear()}`.substr(4 - RegExp.$1.length)
    );
  }
  Object.keys(o).forEach((k) => {
    if (new RegExp(`(${k})`).test(fmt)) {
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? o[k] : `00${o[k]}`.substr(`${o[k]}`.length)
      );
    }
  });
  return fmt;
};

/**
 * @name: 计算加
 * @param {*} a
 * @param {*} b
 * @return {*}
 */
export const calcAdd = (a, b) => {
  var c, d, e;
  try {
    c = a.toString().split('.')[1].length;
  } catch (f) {
    c = 0;
  }
  try {
    d = b.toString().split('.')[1].length;
  } catch (f) {
    d = 0;
  }
  return (
    (e = Math.pow(10, Math.max(c, d))), (calcMul(a, e) + calcMul(b, e)) / e
  );
};

/**
 * @name: 计算减
 * @param {*} a
 * @param {*} b
 * @return {*}
 */
export const calcSub = (a, b) => {
  var c, d, e;
  try {
    c = a.toString().split('.')[1].length;
  } catch (f) {
    c = 0;
  }
  try {
    d = b.toString().split('.')[1].length;
  } catch (f) {
    d = 0;
  }
  return (
    (e = Math.pow(10, Math.max(c, d))), (calcMul(a, e) - calcMul(b, e)) / e
  );
};

/**
 * @name: 计算乘
 * @param {*} a
 * @param {*} b
 * @return {*}
 */
export const calcMul = (a, b) => {
  var c = 0,
    d = a.toString(),
    e = b.toString();
  try {
    c += d.split('.')[1].length;
  } catch (f) {}
  try {
    c += e.split('.')[1].length;
  } catch (f) {}
  return (
    (Number(d.replace('.', '')) * Number(e.replace('.', ''))) / Math.pow(10, c)
  );
};

/**
 * @name: 计算除
 * @param {*} a
 * @param {*} b
 * @return {*}
 */
export const calcDiv = (a, b) => {
  var c,
    d,
    e = 0,
    f = 0;
  try {
    e = a.toString().split('.')[1].length;
  } catch (g) {}
  try {
    f = b.toString().split('.')[1].length;
  } catch (g) {}
  return (
    (c = Number(a.toString().replace('.', ''))),
    (d = Number(b.toString().replace('.', ''))),
    calcMul(c / d, Math.pow(10, f - e))
  );
};

/**
 * @name: 判断当前日期是今天还是昨天
 * @param {*} time 格式 “2022-06-01 15:16:30”
 * @return {*}
 */
export const fnTime = (time) => {
  let ptime = new Date(time).getTime();

  const twentyFourHours = 24 * 60 * 60 * 1000;
  // const fortyEightHours = 24 * 60 * 60 * 1000 * 2;
  const date = new Date();
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const today = `${year}-${month}-${day}`;
  const todayTime = new Date(today).getTime();
  const yesterdayTime = new Date(todayTime - twentyFourHours).getTime();

  // const lastYesterdayTime = new Date(todayTime - fortyEightHours).getTime();
  // else if (ptime < yesterdayTime && lastYesterdayTime <= ptime) {
  //   return '前天';
  // }

  if (ptime >= todayTime) {
    return '今天';
  } else if (ptime < todayTime && yesterdayTime <= ptime) {
    return '昨天';
  } else {
    return time;
  }
};
